package com.lanou.zy.projecta.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.CommunityExpandableListViewAdapter;
import com.lanou.zy.projecta.entity.Community;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/22.
 */
public class CommunityFragment extends Fragment {
    private ExpandableListView communityExpandableListView;
    private CommunityExpandableListViewAdapter communityExpandableListViewAdapter;
    private List<Community.DataEntity.ForumListEntity> cdf;
    private Community.DataEntity cd;

    private RequestQueue queue;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_community, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initView(View view) {
        communityExpandableListViewAdapter = new CommunityExpandableListViewAdapter(getActivity());
        communityExpandableListView = (ExpandableListView) view.findViewById(R.id.communityExpandableListView);
        communityExpandableListView.setGroupIndicator(null);
        communityExpandableListView.setAdapter(communityExpandableListViewAdapter);
        communityExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
    }

    private void initData() {

        queue = Volley.newRequestQueue(getActivity());
        String url = "http://open.qyer.com/qyer/bbs/entry?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883641&lon=121.544679";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Community c = gson.fromJson(response, Community.class);
                cd = c.getData();
                cdf = cd.getForum_list();
                for (int i = 0; i < cdf.size(); i++) {
                    communityExpandableListView.expandGroup(i);
                }
                communityExpandableListViewAdapter.addData(cdf);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (queue != null) {
            queue.cancelAll(getActivity());
        }
    }


}
